import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import Login from "@/views/Login.vue";
import Home from "@/views/Home.vue";
import { getItem } from "@/utils/localDate";
import List from "@/views/Post/List.vue";
import Publish from "@/views/Post/Publish.vue";
import Category from "@/views/category/Category.vue";
// 5. 创建路由实例
const router = new VueRouter({
  routes: [
    { path: "/", redirect: "/home" },
    { path: "/login", component: Login },

    {
      path: "/home",
      component: Home,
      children: [
        { path: "postlist", component: List },
        // ？说明前面的参数可以没有，也可以有一个
        { path: "postpublish/:id?", component: Publish },
        { path: "category", component: Category },
      ],
    },
  ],
});

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next();
  } else {
    if (getItem()) {
      next();
    } else {
      next("/login");
    }
  }
});
export default router;
